package com.zyz.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GetQrCodeBase64 {

	//获取二维码接口连接地址
	private static String url = "http://127.0.0.1:8080/cssjzsgj/application/getQrCodeImage.action";
	
	public static void main(String[] args) {
		getQrCodeImage();
	}
	
	/**
	 * 获取二维码图片的方法
	 */
	public static void getQrCodeImage(){
		  HttpClient httpClient = new DefaultHttpClient();
		  HttpResponse response = null;  
		try {
			 //获取链接
			 HttpPost httpPost = new HttpPost(url);
			 //Content-Type是application/x-www-form-urlencoded
			 httpPost.setHeader("Content-Type","application/json;charset=utf-8");
			 List<NameValuePair> params = new ArrayList<NameValuePair>();
			 // userNme表示用户名,systemName表示系统名称,qrCodeContent表示二维码内容,这三项为必填项。
			 String userName ="管理员";
			 String systemName ="数据与展示工具系统";
			 String qrCodeContent = "测试获取二维码图片的接口";
			 params.add(new BasicNameValuePair("userName",userName));
			 params.add(new BasicNameValuePair("systemName",systemName));
			 params.add(new BasicNameValuePair("qrCodeContent",qrCodeContent));
			 httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
			
			 response = httpClient.execute(httpPost);
			 int statusCode =response.getStatusLine().getStatusCode();
			 String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");
			 HashMap<String, String> map = new HashMap<String, String>();
			 Gson gson = new Gson();
			 map=gson.fromJson(jsonStr,  new TypeToken<HashMap<String,Object>>(){}.getType());
			 String qrCodeBase64 = map.get("qrCode").toString();
			 System.out.println("返回的状态码:"+statusCode+" , 返回二维码图片的base64编码:"+qrCodeBase64);
		}catch(Exception e) {
			e.printStackTrace();
		}finally{
			if(response!=null){
				//response.
			}
		}
	}

}
